<?php
declare (strict_types = 1);

namespace app\api\model\v1;

use think\Model;

/**
 * @mixin \think\Model
 * 样本点
 */
class ProjectSample extends AppModel
{
    //关联关系


    //所属区域
    public function area(){
        return $this->belongsTo(Area::class,'area_id');
    }

    //所属街道
    public function street(){
        return $this->belongsTo(Street::class,'street_id');
    }

    //社区
    public function community(){

        return $this->belongsTo(Community::class,'community_id');
    }

    public function project(){

        return $this->belongsTo(Project::class,'project_id');
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/29 15:37
     * @param $arrData
     * @param $create_id
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 处理样本点
     */
    public static function saveSampleExcel($arrData, $create_id){
        if (!empty($arrData)) {
            foreach ($arrData as $key => $value) {  //社区
                $community_res = Community::where(['community_title'=>trim($value['a'])])->find();
                if (!is_null($community_res)) {
                    $community_id = $community_res->id;
                    $street_id = $community_res->street_id;
                    $area_id = $community_res->area_id;
                    if (isset($value['item'])) { //判断是否包含item字段 为项目
                        foreach ($value['item'] as $child_key => $child_value) {
                            $project_res = Project::where(['project_title'=>trim($child_value['b'])])->find();
                            if (!is_null($project_res)) {
                                $project_id = $project_res->id;
                                if (isset($child_value['item'])) { //判断是否包含item字段 为样本点
                                    foreach ($child_value['item'] as $sample_key => $sample_value) {
                                        $project_sample_res = self::where(['sample_title'=>trim($sample_value['c'])])->find();
                                        if (is_null($project_sample_res)) {
                                            self::create([
                                                'sample_title'=>trim($sample_value['c']),
                                                'area_id'=>$area_id,
                                                'street_id'=>$street_id,
                                                'community_id'=>$community_id,
                                                'project_id'=>$project_id,
                                                'uid'=>$create_id,
                                                'address'=>$sample_value['d']
                                            ]);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
            $result = 200;
        }else{
            $result = 500;
        }
        return $result;
    }
}
